package com.shinbada.modules.competitor.web;

import com.shinbada.excel.modle.ImportExcelResult;
import com.shinbada.excel.modle.UploadModle;
import com.shinbada.modules.competitor.entity.CompetitorProductDataItem;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;

import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import com.google.common.collect.Lists;
import com.shinbada.common.utils.DateUtils;
import com.shinbada.common.json.AjaxJson;
import com.shinbada.core.persistence.Page;
import com.shinbada.core.web.BaseController;
import com.shinbada.common.utils.StringUtils;
import com.shinbada.modules.competitor.entity.CompetitorNoumenonDataItem;
import com.shinbada.modules.competitor.service.CompetitorNoumenonDataItemService;

import com.shinbada.excel.ExcelKit;

/**
 * 本体-数据项Controller
 * @author initcap
 * @version 2021-08-11
 */
@RestController
@RequestMapping(value = "/competitor/competitorNoumenonDataItem")
@Api(tags = "本体-数据项")
@ApiSupport(author = "initcap", order = 1)
public class CompetitorNoumenonDataItemController extends BaseController {

	@Autowired
	private CompetitorNoumenonDataItemService competitorNoumenonDataItemService;

	@ModelAttribute
	public CompetitorNoumenonDataItem get(@RequestParam(required=false) String id) {
		CompetitorNoumenonDataItem entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = competitorNoumenonDataItemService.get(id);
		}
		if (entity == null){
			entity = new CompetitorNoumenonDataItem();
		}
		return entity;
	}

	@RequiresPermissions("user")
	@GetMapping("list")
	@ApiOperation("本体-数据项列表数据")
	public AjaxJson list(CompetitorNoumenonDataItem competitorNoumenonDataItem, HttpServletRequest request, HttpServletResponse response) {
		Page<CompetitorNoumenonDataItem> page = competitorNoumenonDataItemService.findPage(new Page<>(request, response), competitorNoumenonDataItem);
		return AjaxJson.success().put("page",page);
	}

	@RequiresPermissions("user")
	@GetMapping("queryById")
	@ApiOperation("根据Id获取本体-数据项数据")
	public AjaxJson queryById(CompetitorNoumenonDataItem competitorNoumenonDataItem) {
		return AjaxJson.success().put("competitorNoumenonDataItem", competitorNoumenonDataItem);
	}

	@RequiresPermissions(value={"competitor:competitorNoumenonDataItem:add","competitor:competitorNoumenonDataItem:edit"},logical=Logical.OR)
	@PostMapping("save")
	@ApiOperation("保存本体-数据项")
	public AjaxJson save(CompetitorNoumenonDataItem competitorNoumenonDataItem) {
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(competitorNoumenonDataItem);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		competitorNoumenonDataItemService.save(competitorNoumenonDataItem);
		return AjaxJson.success("保存本体-数据项成功");
	}

	@RequiresPermissions("competitor:competitorNoumenonDataItem:del")
	@DeleteMapping("delete")
	@ApiOperation("批量删除本体-数据项")
	public AjaxJson delete(String ids) {
		String[] idArray =ids.split(",");
		for(String id : idArray){
			competitorNoumenonDataItemService.delete(competitorNoumenonDataItemService.get(id));
		}
		return AjaxJson.success("删除本体-数据项成功");
	}

	@RequiresPermissions("competitor:competitorNoumenonDataItem:export")
	@GetMapping("export")
	@ApiOperation("导出本体-数据项")
	public void export(CompetitorNoumenonDataItem entity, HttpServletResponse response) {
		ExcelKit.$Export(CompetitorNoumenonDataItem.class, response).downXlsx(competitorNoumenonDataItemService.findList(entity), false);
	}

	@RequiresPermissions("competitor:competitorNoumenonDataItem:import")
	@GetMapping("template")
	@ApiOperation("导出本体-数据项模版")
	public void export(HttpServletResponse response) {
		ExcelKit.$Export(CompetitorNoumenonDataItem.class, response).downXlsx(new ArrayList<>(), true);
	}

	@RequiresPermissions("competitor:competitorNoumenonDataItem:import")
	@PostMapping(value = "import")
	@ApiImplicitParam(name = "modle", paramType = "query", allowableValues = "skip,exit,cover")
	@ApiOperation("导入文件,下载模版为/export")
	public AjaxJson uploadExcel(MultipartFile file, UploadModle modle) {
		if (shinProperites.isDemoMode()) {
			return AjaxJson.error("演示模式，不允许操作！");
		}
		ImportExcelResult<CompetitorNoumenonDataItem> result = competitorNoumenonDataItemService.uploadExcel(file, modle);
		return AjaxJson.success(result);
	}
	@RequiresPermissions("user")
	@GetMapping("getDataItemValueByTypeAndName")
	@ApiOperation("根据数据项类型和数据项获取本体数据项集")
	public AjaxJson getTypeAndNameByDataItemValue(CompetitorProductDataItem competitorProductDataItem) {
		List<CompetitorNoumenonDataItem> competitorNoumenonDataItemList = competitorNoumenonDataItemService.getTypeAndNameByDataItemValue(competitorProductDataItem);
		return AjaxJson.success().put("data",competitorNoumenonDataItemList);
	}
	@RequiresPermissions("user")
	@GetMapping("getDataItemValueByTypeAndNames/{names}")
	@ApiOperation("根据数据项名称数据获取数值")
	public AjaxJson getDataItemValueByTypeAndNames(@PathVariable("names") String[] names) {
		Map<String,List<CompetitorNoumenonDataItem>> map = competitorNoumenonDataItemService.getTypeAndNameByDataItemValues(names);
		return AjaxJson.success().put("data",map);
	}
}
